package com.voltmao.energy.Event;

import java.util.Map;
import java.util.Set;

import com.google.common.eventbus.EventBus;

public class MyEventBus {
	/**
	 * 变量名称最好对应xml注入中property属性的名称
	 */
	private static EventBus eventBus = new EventBus();
	
	private Map<EventBus, EventListener> eventMap;
	
	public Map<EventBus, EventListener> getEventMap() {
		return eventMap;
	}

	public void setEventMap(Map<EventBus, EventListener> eventMap) {
		this.eventMap = eventMap;
	}
	
	/**
	 * 对应的事件注册方法，取名自定义，对应xml注入
	 */
	public void register(){
		Set<EventBus> eventBusSet = eventMap.keySet();
		for(EventBus eventBusItem : eventBusSet){
			System.out.println("toBeRegistered:"+eventMap.get(eventBusItem));
			eventBus.register(eventMap.get(eventBusItem));//注意一定要匹配eventBus和post相同EventBus对象
		}
	}
	/**
	 * 调用此方法传输到事件中
	 * @param obj：对应类型的数据
	 */
	public static void post(Object obj){
		eventBus.post(obj);
	}
	//手动执行的注册与反注册
	public static void register(Object obj){
		eventBus.register(obj);
	}
	public static void unregister(Object obj){
		eventBus.unregister(obj);
	}
	public static EventBus getEventBus(){
		return eventBus;
	}
}
